package com.budwk.app.sqfwV2.generator.caringStaff;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.constants.SqfwFormSummaryConstants;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.models.Sqfw_form_summary_pdf;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.SqfwPdfCreateCommonBO;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CompleteFlowBusinessParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwV2.dto.BriefDTO;
import com.budwk.app.sqfwV2.dto.CaringStaffDTO;
import com.budwk.app.sqfwV2.dto.DifficultPersonDTO;
import com.budwk.app.sqfwV2.dto.dto.InterviewRecordDTO;
import com.budwk.app.sqfwV2.dto.dulaying.DuLaYingPropagandaOrScreeningPdfDTO;
import com.budwk.app.util.ClassUtil;
import com.budwk.app.util.poiUtils.CommonFormOfficeUtils;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.deepoove.poi.policy.RenderPolicy;
import com.google.common.collect.Maps;
import org.nutz.dao.Cnd;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class DifficultPersonPdfGenerator extends BasePdfGenerator {

    private String templateName;

    private String downName;

    private List<Sqfw_formSummary_new> sqfwFormSummaryNews;

    public void setDownName(String downName) {
        this.downName = downName;
    }

    public String getDownName() {
        return downName;
    }

    public DifficultPersonPdfGenerator(){}


    public void setSqfwFormSummaryNews( List<Sqfw_formSummary_new> sqfwFormSummaryNews){
        this.sqfwFormSummaryNews =sqfwFormSummaryNews;
    }

    public DifficultPersonPdfGenerator(String templateName){
        this.templateName=templateName;
    }



    public String getTemplateName() {
        return this.templateName;
    }


    public boolean pdfCreateConditionCheck(SqfwPdfCreateCommonBO pdfDTO) {
        return false;
    }




    @Override
    protected boolean pdfCreateConditionCheck(PdfGenerateParam pdfGenerateParam) {
        return true;
    }

    @Override
    protected void customerPopularPdfParam(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {
        CompleteFlowBusinessParam businessParam = (CompleteFlowBusinessParam) pdfGenerateParam.getBusinessParam();
        DifficultPersonDTO difficultPersonDTO = (DifficultPersonDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();
        SqfwFormsummaryNewService sqfwFormsummaryNewService = NutzIocUtils.getBean(SqfwFormsummaryNewService.class);

        List<Sqfw_formSummary_new> sqfwFormSummaryNews = sqfwFormsummaryNewService.query(Cnd.where("param_data->>'id'","=",difficultPersonDTO.getId())
                .and(Sqfw_formSummary_new::getType,"=", SqfwFormSummaryConstants.DIFFICULT_PERSON)
                .and(Sqfw_formSummary_new::getCommunity,"=",businessParam.getFormSummary().getCommunity())
                .asc(Sqfw_formSummary_new::getFillDate));
       // paramMap.put("fillDate", DateUtil.format(difficultPersonDTO.getFillDate(), "yyyy年MM月dd日"));

        if(IdcardUtil.isValidCard(difficultPersonDTO.getIdNumber())){
            paramMap.put("age", IdcardUtil.getAgeByIdCard(difficultPersonDTO.getIdNumber()));
            paramMap.put("gender", IdcardUtil.getGenderByIdCard(difficultPersonDTO.getIdNumber())==1?"男":"女");
        }else {
            paramMap.put("age",  DateUtil.ageOfNow(difficultPersonDTO.getDateOfBirth()));
            paramMap.put("gender",difficultPersonDTO.getGender());
        }

        List<InterviewRecordDTO> interviewRecordDTOS=new ArrayList<>();

        for (Sqfw_formSummary_new sqfwFormSummaryNew : sqfwFormSummaryNews) {
            InterviewRecordDTO recordDTO=new InterviewRecordDTO();
            DifficultPersonDTO difficultPersonDTO1 = JSONObject.parseObject(sqfwFormSummaryNew.getParamData(), DifficultPersonDTO.class);
            recordDTO.setFillDate(DateUtil.format(difficultPersonDTO1.getFillDate(), "yyyy年MM月dd日"));
            recordDTO.setVisitRecord(difficultPersonDTO1.getVisitRecord());
            if(StrUtil.isNotEmpty( difficultPersonDTO1.getCollectorSignature())){
                //走访人签字
                recordDTO.setCollectorSignature(CommonFormOfficeUtils.addWordPicture( difficultPersonDTO1.getCollectorSignature(), 2.0F));
            }else {
                recordDTO.setCollectorSignature(null);
            }
            if(StrUtil.isNotEmpty( difficultPersonDTO1.getObserverSignature())){
                //走访人签字
                recordDTO.setObserverSignature(CommonFormOfficeUtils.addWordPicture( difficultPersonDTO1.getObserverSignature(), 2.0F));
            }else {
                recordDTO.setObserverSignature(null);
            }
            if(StrUtil.isNotEmpty( difficultPersonDTO1.getCheckedUnitLeader())){
                //走访人签字
                recordDTO.setCheckedUnitLeader(CommonFormOfficeUtils.addWordPicture( difficultPersonDTO1.getCheckedUnitLeader(), 2.0F));
            }else {
                recordDTO.setCheckedUnitLeader(null);
            }
            interviewRecordDTOS.add(recordDTO);
        }


//        if(StrUtil.isNotEmpty( difficultPersonDTO.getCollectorSignature())){
//            //走访人签字
//            paramMap.put("collectorSignature",CommonFormOfficeUtils.addWordPicture( difficultPersonDTO.getCollectorSignature(), 2.0F) );
//        }else {
//            paramMap.put("collectorSignature",null);
//        }
//
//        if(StrUtil.isNotEmpty( difficultPersonDTO.getObserverSignature())){
//            //旁听人签字
//            paramMap.put("observerSignature",CommonFormOfficeUtils.addWordPicture( difficultPersonDTO.getObserverSignature(), 2.0F) );
//        }else {
//            paramMap.put("observerSignature",null);
//        }
//
//        if(StrUtil.isNotEmpty( difficultPersonDTO.getCheckedUnitLeader())){
//            //被访视人签名
//            paramMap.put("checkedUnitLeader",CommonFormOfficeUtils.addWordPicture( difficultPersonDTO.getCheckedUnitLeader(), 2.0F) );
//        }else {
//            paramMap.put("checkedUnitLeader",null);
//        }
        paramMap.put("interviewRecordDTOS",interviewRecordDTOS);

    }


    @Override
    protected String getTemplatePath(PdfGenerateParam pdfGenerateParam) {
        return "sqfwV2/caringStaff/knry.docx";
    }


    protected void createXWPDFTemplateAfter(XWPFTemplate template, PdfGenerateParam pdfGenerateParam) {

    }

    protected Map<String, RenderPolicy> createCustomerRenderPolicy(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {
        Map<String,RenderPolicy> renderPolicyMap = Maps.newHashMap();
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
        renderPolicyMap.put("interviewRecordDTOS",policy);
        return renderPolicyMap;
    }
}
